package com.youxue.teachin.common.utils;

import android.content.Context;
import android.content.SharedPreferences;
import com.library.uiframework.utils.ContextUtils;

/**
 * HomePreferencesUtils
 */
public class PreferencesUtils {

    public static String getString(String key, String defaultValue) {
        return getPreferences().getString(key, defaultValue);
    }

    public static void delete(String key) {
        getPreferences().edit().remove(key).commit();
    }

    public static String getString(String name, String key, String defaultValue) {
        return getPreferences(name).getString(key, defaultValue);
    }

    public static int getInt(String key, int defaultValue) {
        return getPreferences().getInt(key, defaultValue);
    }

    public static boolean getBoolean(String key, boolean defaultValue) {
        return getPreferences().getBoolean(key, defaultValue);
    }

    public static void saveBoolean(String key, boolean value) {
        SharedPreferences.Editor editor = getPreferences().edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public static void saveString(String name, String key, String value) {
        SharedPreferences.Editor editor = getPreferences(name).edit();
        editor.putString(key, value);
        editor.apply();
    }

    public static void saveString(String key, String value) {
        SharedPreferences.Editor editor = getPreferences().edit();
        editor.putString(key, value);
        editor.apply();
    }

    public static void saveInt(String key, int value) {
        SharedPreferences.Editor editor = getPreferences().edit();
        editor.putInt(key, value);
        editor.apply();
    }

    private static SharedPreferences getPreferences() {
        return ContextUtils.getContext().getSharedPreferences("common", Context.MODE_PRIVATE);
    }

    private static SharedPreferences getPreferences(String name) {
        return ContextUtils.getContext().getSharedPreferences(name, Context.MODE_PRIVATE);
    }
}
